package product;

import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.RowSorter.SortKey;
import javax.swing.SortOrder;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;

public class DetailStock extends JFrame {
	private JScrollPane scrollPane;
	private JTable table1;
	DefaultTableModel model;
	public JTable table;
	Vector<String> v=new Vector<String>();
	Vector<String> col = new Vector<String>();
	ArrayList<ProductData> plist=new ArrayList<ProductData>();//생산
	ArrayList<SaleData> slist=new ArrayList<SaleData>();//판매
	ArrayList<DetailStockData2> detail=new ArrayList<DetailStockData2>(); 
	
	int total = 0;
	int index;
	
	
	/**
	 * Launch the application.
	 */
	/**
	 * Create the frame.
	 */
	public DetailStock(int index, JTable table){//Vector<String> v){
		this.index = index;
		this.table = table;
		this.model=model;
		col.add("제품코드");
		col.add("제품명");
		col.add("생산량");
		col.add("판매량");
		col.add("재고량");
		col.add("일자");
//		col.add("판매일자");
		model=new DefaultTableModel(col,0);
		try {
			load();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 680, 480);
		getContentPane().setLayout(null);
		getContentPane().add(getScrollPane());
		tb();
	}

	public void load() throws Exception {// 모든 Exception를 던진다.

		FileInputStream fr = new FileInputStream("C:\\DetailStockList.dat");
		ObjectInputStream br = new ObjectInputStream(fr);
		detail = (ArrayList)br.readObject();
		br.close();
		fr.close();
	}

	public void set2(){
		
		model.addRow(v);
	}
	public void set(){
		try {
			for(int i=0;i<plist.size();i++){
				ProductData value = plist.get(i);
				SaleData val=slist.get(i);
				String code = value.code;//코드
				String name = value.name;//품명
			    int price = value.price;//단가
				int proEa = value.volume;//생산량
				String pronal=value.getNal();//생산일자
				
//				String pkey =(String) table.getValueAt(i, 0);
				String snal=slist.get(i).saleDate;
				int saleEa=0;
//				String snal="";
//				String skey=val.getProCode();
//				if(pkey.equals(skey)){
//					saleEa=slist.get(i).getSaleEa();//판매량
//					snal=slist.get(i).saleDate;//판매일자
//				}
//				int saleEa = slist.get(i).getProTot();//판매량
				Vector<String> row2=new Vector<String>();
//					saleEa = map.get(key).getSaleEa();
					row2.add(code);
					row2.add(name);
					row2.add(proEa+"");
					row2.add(saleEa+"");
					row2.add((proEa-saleEa)+"");
					row2.add(pronal);
					row2.add(snal);
					table1.getRowSorter().toggleSortOrder(6);
					model.addRow(row2);	
			}
		}catch(Exception e){
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	public void tb(){
		for(int i=0;i<detail.size();i++){
				DetailStockData2 ds=detail.get(i);
				String code =ds.getCode();//코드
				String name = ds.getName();//품명
				int proEa = ds.getVolume();//생산량
				int salEa=ds.getSaleEa();
				int stock=proEa-salEa;
				String nal=ds.getNal();
				System.out.println(index);
				if(table.getValueAt(index, 0).equals(name)){
					System.out.println("이프문 들어옴");
				Vector<String> row2=new Vector<String>();
					row2.add(code);
					row2.add(name);
					row2.add(proEa+"");
					row2.add(salEa+"");
//					sd.setProEa(proEa-saleEa);
//					System.out.println((String)table.getValueAt(0, 2));
//					total = k
//					table.setValueAt(total,i, 4);
					row2.add(total+"");
					row2.add(nal);
				
					TableRowSorter sorter = (TableRowSorter)table1.getRowSorter();
					SortKey sortKey = new SortKey(5, SortOrder.ASCENDING);
					List<SortKey> keys = new ArrayList<>();
					keys.add(sortKey);
					sorter.setSortKeys(keys);
					sorter.sort();
					model.addRow(row2);	
			}
		}
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(12, 33,700,500);
			scrollPane.setViewportView(getTable1());
		}
		return scrollPane;
	}
	private JTable getTable1() {
		if (table1 == null) {
			table1 = new JTable(model);
			table1.setAutoCreateRowSorter(true);
			TableRowSorter tableSorter = new TableRowSorter(table1.getModel());
			table1.setRowSorter(tableSorter);
		}
		return table1;
	}
}
